package problem725;

//725.分隔链表
//https://leetcode.cn/problems/split-linked-list-in-parts/

class Solution {
    public ListNode[] splitListToParts(ListNode head, int k) {
        int len = 0;
        ListNode cur = head;
        while(cur != null) {
            cur = cur.next;
            len++;
        }
        ListNode[] ret = new ListNode[k];
        int x = len / k, y = len % k, index = 0;
        cur = head;
        ListNode prev = null;
        while(cur != null) {
            ret[index++] = cur;
            for(int i = 0; i < (y > 0 ? x + 1 : x); i++) {
                prev = cur;
                cur = cur.next;
            }
            y--;
            prev.next = null;
        }
        return ret;
    }
}